package com.example.service;

import com.example.entity.Account;
import com.example.entity.Salary;
import com.example.entity.User;
import com.example.mapper.SalaryMapper;
import com.example.mapper.UserMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SalaryService {

    @Resource
    SalaryMapper salaryMapper;
    @Resource
    private UserMapper  userMapper;

    public void add(Salary salary) {
        Integer userId = salary.getUserId();
        String userIdStr = userId.toString();
        User user = userMapper.selectById(userIdStr);
        salary.setDepartmentId(user.getDepartmentId());
        salaryMapper.insert(salary);
    }

    public void update(Salary salary) {
        salaryMapper.updateById(salary);
    }

    public void deleteById(Integer id) {
        salaryMapper.deleteById(id);
    }

    public List<Salary> selectAll(Salary salary) {
        return salaryMapper.selectAll(salary);
    }

    public List<Salary> selectYear(Salary salary) { return salaryMapper.selectYear(salary);}

    public PageInfo<Salary> selectPage(Integer pageNum, Integer pageSize, Salary salary) {
        Account currentUser = TokenUtils.getCurrentUser();
        if ("USER".equals(currentUser.getRole())) {
            salary.setUserId(currentUser.getId());
        }
        // 开启分页查询
        PageHelper.startPage(pageNum, pageSize);
        List<Salary> list = salaryMapper.selectAll(salary);
        return PageInfo.of(list);
    }

    public Double selectAvgPrice(String year,Integer departmentId) {
        return salaryMapper.selectAvgPrice(year, departmentId);
    }
}